package edu.rakovic.elearning.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import edu.rakovic.elearning.interfaces.Application;
import edu.rakovic.elearning.interfaces.Course;
import edu.rakovic.elearning.interfaces.User;


public class ApplicationsBean implements Applications {
	
	public static long nextId = 1;
	
	private Map<String, Application> applications;
	
	public ApplicationsBean(){
		this(new HashMap<String, Application>());
	}
	
	public ApplicationsBean(HashMap<String, Application> applications) {
		  this.applications = applications;
	}


	public Collection<Application> retreiveApplications() {
		return applications.values();
	}
	
	public Collection<Application> retrieveStudentApplications(String userName) {
		ArrayList<Application> retval = new ArrayList<Application>();
		// Using foreach for filtering.
		for (Application application : applications.values()) {
			if (application.getUser().getUserName().equalsIgnoreCase(userName)) {
				retval.add(application);
			}
		}
		return retval;
	}
	
	public Collection<Application> retrieveCourseApplications(String courseName) {
		ArrayList<Application> retval = new ArrayList<Application>();
		// Using foreach for filtering.
		for (Application application : applications.values()) {
			if (application.getCourse().getName().equalsIgnoreCase(courseName)) {
				retval.add(application);
			}
		}
		return retval;
	}
	
	public Application retreiveSingleApplication(String id) {
		return applications.get(id);
	}
	
	public Application delete(String id) {
		return applications.remove(id);
	}
	
	public Application save(Application application) {
		application.setId("Application" + nextId++);
		return applications.put(application.getId(), application);
	}

}
